#!/bin/bash
set -ex
rm -rf build_protoc && mkdir build_protoc && cd build_protoc
cmake .. -GNinja \
    -DCMAKE_CXX_FLAGS_RELEASE="-O2 -DNDEBUG -Wno-unused-command-line-argument" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_CXX_STANDARD=17 \
    -Dprotobuf_INSTALL=OFF \
    -Dprotobuf_BUILD_TESTS=OFF \
    -Dprotobuf_BUILD_LIBUPB=OFF
ninja protoc
cd ..

OHOS_NDK_HOME=/Applications/DevEco-Studio.app/Contents/sdk/default/openharmony
INSTALL_PREFIX=$(pwd)/../sample/PbCpp/third_party/libprotobuf-lite
export PATH=$OHOS_NDK_HOME/native/build-tools/cmake/bin:$PATH
rm -rf build_pblite && mkdir build_pblite && cd build_pblite
# 构建动态库版本
cmake .. -GNinja \
    -DCMAKE_CXX_FLAGS_RELEASE="-O2 -DNDEBUG -Wno-unused-command-line-argument" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_CXX_STANDARD=17 \
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
    -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
    -DBUILD_SHARED_LIBS=OFF \
    -DABSL_BUILD_MONOLITHIC_SHARED_LIBS=ON \
    -Dprotobuf_FORCE_FETCH_DEPENDENCIES=ON \
    -Dprotobuf_INSTALL=ON \
    -Dprotobuf_BUILD_TESTS=OFF \
    -Dprotobuf_BUILD_LIBUPB=OFF \
    -Dprotobuf_BUILD_SHARED_LIBS=ON \
    -DOHOS_STL=c++_shared \
    -DOHOS_ARCH=arm64-v8a \
    -DOHOS_PLATFORM=OHOS \
    -DCMAKE_TOOLCHAIN_FILE=$OHOS_NDK_HOME/native/build/cmake/ohos.toolchain.cmake
ninja libprotobuf-lite libprotobuf
ninja install

# 构建静态库版本
# cmake .. -GNinja \
#     -DCMAKE_CXX_FLAGS_RELEASE="-O2 -DNDEBUG -Wno-unused-command-line-argument" \
#     -DCMAKE_BUILD_TYPE=Release \
#     -DCMAKE_CXX_STANDARD=17 \
#     -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
#     -Dprotobuf_INSTALL=OFF \
#     -Dprotobuf_BUILD_TESTS=OFF \
#     -Dprotobuf_BUILD_LIBUPB=OFF \
#     -DOHOS_STL=c++_shared \
#     -DOHOS_ARCH=arm64-v8a \
#     -DOHOS_PLATFORM=OHOS \
#     -DCMAKE_TOOLCHAIN_FILE=$OHOS_NDK_HOME/native/build/cmake/ohos.toolchain.cmake
#     -Dprotobuf_BUILD_SHARED_LIBS=ON \
# ninja libprotobuf-lite
# ninja install
set +ex
